package homework;

import java.util.Arrays;

public class StringUtils {
	public int[] positon(String str1,String str2){
		int[] position = new int[]{};
		int temp =0;
		while(true){
			if(str1.indexOf(str2,temp)>=0){
				 position = Arrays.copyOf(position, position.length+1);
			   position[position.length-1]=str1.indexOf(str2,temp);		
			   temp=position[position.length-1]+str2.length();
			        continue;
			}
			break;
		}
		return position;
	}
	
	  public static int[] indexAll(
		      String str, String key){
		    int[] all= new int[0];//{};
		    int i=-key.length();
		    while(true){
		      i = str.indexOf(key, i+key.length());
		      if(i==-1)
		        break;
		      all = Arrays.copyOf(all, all.length+1);
		      all[all.length-1] = i;
		    }
		    return all;
		  }
	
	public boolean check(String str){
		String str1 = str.substring(str.length()/2);
		System.out.println(str1);
		String str2 = str.substring(0,str.length()/2);
		System.out.println(str2);
		char[] c = str2.toCharArray();
		char[] d = new char[c.length];
		int j=0;
		for(int i=c.length-1;i>=0;i--){
			d[j++]=c[i];
		}
		String str3 = String.copyValueOf(d);
		System.out.println(str3);
		if(str1.equals(str3)){
			return true;
		}else{
			return false;
		}
	}
	
	
	public boolean checkStr(StringBuilder str){
		StringBuilder str1= new StringBuilder();
		String s1=str1.append(str).toString();
		StringBuilder str2= new StringBuilder();
		String s2=str2.append(str.reverse()).toString();
		if(s2.equals(s1)){
			return true;
		}
		return false;
	}
	
	public String sub(String str){
		String[] str1 = str.split("@");
		return str1[0];
	}
	
	public int cale(String str,char c){
		int temp=0;
	  int time=0;
		while(true){
			if(str.indexOf(c,temp)>=0){
				temp=str.indexOf(c,temp)+1;
				time++;
				while(true){
					if(temp>=str.length()){
						break;
					}
					if(str.charAt(temp)==c){
						temp++;
						continue;
					}
					break;
				}
				continue;
			}
			break;
		}
		return time;
	}
	
	  public static int countAll(
		      String str, char ch){
		    int count = 0;
		    for(int i=0; i<str.length(); i++){
		      char c = str.charAt(i);
		      int prev = i==0?-1:str.charAt(i-1);
		      if(c==ch && prev !=ch){
		        count++;
		      }
		    }
		    return count;
		  }
	
	public static void main(String[] args){
		StringUtils stringUtils = new StringUtils();
		int[] position = stringUtils.positon("abcabcabcabc", "ab");
		int[] p = stringUtils.indexAll("abcabcabcabc", "ab");
		System.out.println(Arrays.toString(position));
		System.out.println(Arrays.toString(p));
		//System.out.println(stringUtils.check("abcdeffedcba"));
		System.out.println(stringUtils.checkStr(new StringBuilder("adeeda")));				
		System.out.println(stringUtils.sub("aaaaaa@cc.com"));
		System.out.println(stringUtils.cale("aabbccddaa", 'a'));
		System.out.println(stringUtils.countAll("aaaaabbccddaa", 'a'));

//		String s1 = "cabcabc";
//		String s2 = "ab";
//		System.out.println(s1.indexOf(s2));
	}
}
